﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Practices.Unity;
using Condimar.Entities;
using Condimar.BussinesLogic;

namespace Condimar.UI
{
    public partial class FrmRegistrarVendedor : Form
    {
        [Dependency]
        public IVendedorBL vendedorBL { get; set; }

        bool b;
        public FrmRegistrarVendedor()
        {
            InitializeComponent();
            this.BuildUp();
        }

        public Vendedor CurrentVendedor { get; set; }

        public void Nuevo()
        {
            b = true;
            vendedorBindingSource.AddNew();
        }

        public void Editar()
        {
            b = false;
            vendedorBindingSource.DataSource = CurrentVendedor;
        }
        private void btnGuardar_Click(object sender, EventArgs e)
        {
            Vendedor currentVendedor = vendedorBindingSource.Current as Vendedor;

            if ((nombresTextBox.Text).Length != 0 && (apellidosTextBox.Text).Length != 0 && (dniTextBox.Text).Length != 0 &&
                (direccionTextBox.Text).Length != 0 && (telefonoTextBox.Text).Length != 0)
            {
                if ((dniTextBox.Text).Length == 8)
                {
                    if (currentVendedor != null)
                    {
                        if (b == true)
                        {
                            vendedorBL.AddVendedor(currentVendedor);
                            MessageBox.Show("Registro Correcto", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            Close();
                        }
                        if (b == false)
                        {
                            vendedorBL.UpdateVendedor(currentVendedor);
                            MessageBox.Show("Modificacion Correcta", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            Close();
                        }
                    }
                }
                else
                    MessageBox.Show("Longuitud de Dni incorrecto", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
                MessageBox.Show("Debe completar todos los campos", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void dniTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Char.IsNumber(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        private void nombresTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Char.IsLetter(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        private void apellidosTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Char.IsLetter(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        private void telefonoTextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Char.IsNumber(e.KeyChar) || Char.IsPunctuation(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        private void FrmRegistrarVendedor_Load(object sender, EventArgs e)
        {

        }
    }
}
